#ifndef MSTAR_TV_H
#define MSTAR_TV_H


#ifndef DATA_STRUCT_H
#define DATA_STRUCT_H
#include "data_struct.h"
#endif


//#include "marco_data.h"
#include <QMessageBox>
#include <QFile>
#include<QString>
#include <QVariant>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;


class MS_TV_INDEX{
public :
    int dtv_d;
    int dtv_r;
    int dtv_t;
    int dtv_u;
public:
    int size()
    {
        return 4;
    }

    int  get_item(int i)
    {
        switch(i)
        {
        case 0:
            return dtv_d;
            break;
        case 1:
            return dtv_r;
            break;
        case 2:
            return dtv_t;
            break;
        case 3:
            return dtv_u;
            break;
        }
    }

    void set_item(int i,int num)
    {
        switch(i)
        {
        case 0:
            dtv_d+=num;
            break;
        case 1:
            dtv_r+=num;
            break;
        case 2:
            dtv_t+=num;
            break;
        case 3:
            dtv_u+=num;
            break;
        }
    }


};

class MSTAR_TV
{
    friend class translate;
public:
    MSTAR_TV();
    ~MSTAR_TV();
     //   MSTAR_TV(QString &,QString &,QString &）；
     MSTAR_ATV_DATA *mstar_atv;//存储了所有的ATV频道信息
     MSTAR_DTV_DATA *mstar_dtv_data;
     QList<MSTAR_DTV_DATA *> mstar_dtv;

private:
     //This record the current dtv numbers
     uint count_dtvb;
     uint count_dtvc;
     uint del_count_dtvb;
     uint del_count_dtvc;
     QString dtvb_file;
     QString dtvc_file;
     QString atv_file;
     //This record the original dtv numbers which in tv file.
     //They will be used in Write_DTV_BIN()when datas,edited，write to tv files.
     uint old_cou_dtvb;
     uint old_cou_dtvc;
     uint count_atv;
     uint empty;//record the open tv files times.if(times>1) I let clear datas.


private:
    // bool read_atv;
     uint Read_DTV_Bin(const char*);//dtvb and dtvc read file func
     int Write_DTV_Bin(const char*,const uint  count,uint );
     void Copy_File(const char *src,const char *dst,const uint src_start,const uint dst_start,const uint len);//the two file
     //usually is the same ,but  for more useful with the function,I choice two start variable.
     void Mod_Check(ushort nun1,ushort num2);
     void Swap_ATV_CH(ushort,ushort);
     void correct(ushort,ushort);
     bool Is_Exist_Freq(ushort,bool);
     ushort Get_Empty_Freq(bool);//return table_id;
     bool Mod_Freq(ushort*,uint ,bool);
     int Find_Empty_DTV(int,int *,int);//Find delete=1
     void Sort_DTV();
     void Sort_Num(int ,MS_TV_INDEX);
     void Count_Type(uint start,uint end,MS_TV_INDEX *);
public:
//interface to TCL_MSTAR_ATV_TV  and　TCL_MSTAR_DTV_TV
     MS_TV_INDEX dtvb_type;
     MS_TV_INDEX dtvc_type;

     int Read_ATV(const char *);
     int Read_DTV(const char *);
     int Write_ATV(char *);
     int Write_DTV(char *);
     uint Get_ATV_Count();

     bool Is_Empty();
     void Clear();
     void Clear_Data();
     void Empty_Add_One();
     void Empty_Dul_One();
     void set_Empty(uint);

     uint Get_DTV_Count();
     int Get_Max_ATV_Count();//获取有效数据的最大频道号
     void Del_ATV_Row(int row);
     void Del_DTV_Row(int row,int tv_class);

     void Mod_ATV(int row,int col,QString );
     void Mod_DTV(int row,int col,QString value);
     void Get_Chan_Num(QList<uint> *atv_Num,QMap<uint,uchar>* dtv_air_Num,QMap<uint,uchar> *dtv_cable_num);
     int cal_freq(ushort table_id,bool is_b_c);

     void Add_DTV_CH(QList<QString>&);
     void Add_ATV_CH(QList<QString>&);
     ushort Get_ATV_Num(ushort);
     ushort Get_DTV_Num(ushort);

     //ATV数据对ui_ATVPRIndexTable和ui_ATVPRTableMap进行校验
     void Check_ATV();
     uint  Get_DTV_Sum(int tv_class);
};

#endif // MSTAR_TV_H
